#! /usr/bin/env python
#################################################################################
#     File Name           :     test1.py
#     Created By          :     xiaogebin
#     Creation Date       :     [2023-09-02 20:40]
#     Last Modified       :     [2023-11-05 19:42]
#     Description         :
#################################################################################
import asyncio
import websockets


async def hello():
    uri = "ws://127.0.0.1:10000/wxlink"
    async with websockets.connect(uri) as websocket:
        n = 1

        # 接收服务端的信息
        bytes_data = await websocket.recv()
        print(bytes_data)

        while True:
            # 发送给服务端的信息
            message = input('input: ')
            print(f"发送给服务端的信息：{message}")
            if message == "quit":
                break
            await websocket.send(message)
            while True:
                try:
                    bytes_data = await websocket.recv()
                    str_data = str(bytes_data)
                    if "end" == str_data or "error" == str_data or "stop" == str_data:
                        break
                    print("接收来自服务端的信息:{}".format(str_data))
                except Exception as e:
                    print(e)
                    break

                # bytes 转 string


if __name__ == '__main__':
    r = hello()
    asyncio.run(r)
